<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use App\Models\User;
class login extends Controller{

    //前台登录页面
    function denglu(){
        return view("denglu");
    }

    //前台登录功能
    function se_login(Request $request){
        $row = $request->input();

      $rows=DB::table('user')->where('username',$row['username'])->get();
        foreach($rows as $v);
        $name=isset($v->username)?$v->username:"";
        $pwd=isset($v->password)?$v->password:"";
        if($row['username']!=$name){return ['code'=>'-2','name'=>'用户名不存在，先注册吧'];exit;}
        if($row['password']!=$pwd){return ['code'=>'-2','name'=>'密码错误，请重试'];exit;}
            session(['login_name' => $name]);
            return ['code'=>'1','name'=>'登录成功,点击确认后跳转首页','url'=>'/index'];

        }


    function sign_out(Request $request){
        $request->session()->flush();
        return ['code'=>'1','name'=>'退出登录成功','url'=>'/index'];
    }

















    //注册页面
    function register(){
        return view('register');
    }

    //注册功能
    function se_register(Request $request){
       $row = $request->input();
       $model=new User();
       if(!$row['username']){return ['code'=>'-1','name'=>'用户名不能为空'];exit;}
       if(!$row['password']){return ['code'=>'-1','name'=>'密码不能为空'];exit;}
       if(!$row['userphone']){return ['code'=>'-1','name'=>'电话号码不能为空'];exit;}
       $res=DB::table('user')->insert(['username'=>$row['username'],'password'=>$row['password'],'userphone'=>$row['userphone']]);
       if($res){
           return ['code'=>'1','name'=>'注册成功,点击确认后跳转登录页','url'=>'/denglu'];
        }
           return ['code'=>'-1','name'=>'注册失败'];
    }
}
?>
